#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>

int sm3_hash(const unsigned char *data, size_t len, unsigned char *out) {
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
    const EVP_MD *md = EVP_sm3();
    if (!ctx || !md) return -1;

    if (EVP_DigestInit_ex(ctx, md, NULL) != 1 ||
        EVP_DigestUpdate(ctx, data, len) != 1 ||
        EVP_DigestFinal_ex(ctx, out, NULL) != 1) {
        EVP_MD_CTX_free(ctx);
        return -1;
    }
    EVP_MD_CTX_free(ctx);
    return 0;
}

int main() {
    const char *msg = "fsl";
    unsigned char hash[32]; // SM3 输出 256 位 = 32 字节

    if (sm3_hash((unsigned char*)msg, strlen(msg), hash) == 0) {
        for (int i = 0; i < 32; i++) {
            printf("%02x", hash[i]);
        }
        printf("\n");
    }
    return 0;
}
